package me.aip.mov.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.aip.mov.controller.vo.TypeCate;
import me.aip.mov.entity.Type;
import me.aip.mov.mapper.TypeMapper;
import me.aip.mov.service.ITypeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aip9105
 * @since 2022-01-27
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements ITypeService {

    @Override
    public List<Type> findTypes(Integer pid) {
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("type_id","type_name");
        queryWrapper.eq("type_pid",pid);
        queryWrapper.eq("type_status",1);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<Type> findLabels() {
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("type_id","type_name");
        queryWrapper.eq("type_pid",0);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<TypeCate> findCates() {
        List<Type> types = this.findTypes(0);
        List<TypeCate> cates = new ArrayList<>();
        for (Type type : types) {
            List<Type> childTypes = this.findTypes(type.getTypeId());
            cates.add(TypeCate
                    .builder()
                    .type(type)
                    .childTypes(childTypes)
                    .build());
        }
        return cates;
    }
}
